﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "资源管理";
}
<div id="page-header" class="clearfix">
    <div class="page-header">
        <h2>资源管理</h2>
        <span class="txt">可以增删查改一个系统的资源</span>
    </div>
</div>

<div class="panel panel-default toggle panelMove panelClose panelRefresh">
    <!-- Start .panel -->
    <div class="panel-heading">
        <h4 class="panel-title">查询条件</h4>
    </div>
    <div class="panel-body pt0 pb0">
        <form class="form-horizontal group-border">
            @Html.AntiForgeryToken()
            <div class="form-group col-lg-4 col-md-6">
                <label class="col-lg-3 col-md-3 control-label" for="">所属系统</label>
                <div class="col-lg-9 col-md-9">
                    @Html.DropDownList("ddlApplicationId", (IEnumerable<SelectListItem>)@ViewBag.ApplicationId, new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group col-lg-4 col-md-6">
                <label class="col-lg-3 col-md-3 control-label" for="">资源名称</label>
                <div class="col-lg-9 col-md-9">
                    <input id="txtResourceName" type="text" class="form-control" name="default">
                </div>
            </div>
            <div class="form-group col-lg-4 col-md-6">
                <label class="col-lg-3 col-md-3 control-label" for="">资源类型</label>
                <div class="col-lg-9 col-md-9">
                    @Html.DropDownList("ddlType", (IEnumerable<SelectListItem>)@ViewBag.Type, new { @class = "form-control" })
                </div>
            </div>
        </form>
    </div>
    <div class="panel-footer">
        <a class="btn btn-primary button-floatRight" href="@Url.Action("Create")">添加</a>
        <button id="btnReset" type="reset" class="btn btn-danger button-floatRight">重置</button>
        <button id="btnQuery" type="submit" class="btn btn-primary button-floatRight">查询</button>
    </div>
</div>

<div class="panel panel-default toggle panelMove panelClose panelRefresh">
    <div class="panel-heading">
        <h4 class="panel-title">查询结果</h4>
    </div>
    <div class="panel-body">
        <table id="ResourceTable" class="table table-striped table-hover dt-responsive display nowrap" cellspacing="0" width="100%"></table>
    </div>
</div>
<script>
    $(document).ready(function () {
        Resources.Search();

        $("#btnQuery").click(function () {
            Resources.Search();
        });

        $("#btnReset").click(function () {
            $("form").each(function () {
                this.reset();
            });
        });
    });

    var Resources = {
        Search: function () {
            $("#ResourceTable").mainToolDataTable({
                "ajax": {
                    url: '@Url.Action("PageResources")',
                    type: 'POST',
                    data: { filter: { ApplicationId: $("#ddlApplicationId").val(), Type: $("#ddlType").val(), Name: $("#txtResourceName").val() } }
                },
                "columns": [
                    { "data": "ApplicationName", "title": "应用名称" },
                    { "data": "Code", "title": "资源编码" },
                    { "data": "Name", "title": "资源名称" },
                    { "data": "TypeName", "title": "资源类型" },
                    { "data": "Order", "title": "资源排序" },
                    { "data": "Url", "title": "资源URL" },
                    {
                        "data": "PermissionNames",
                        "title": "包含权限",
                        "render": function (data, type, row) {
                            var str = "";
                            data.forEach(function (e) { str += "["+e + "]" })
                            return str;
                        }
                    },
                    {
                        title: "操作",
                        orderable: false,
                        data: "Id",
                        style: "min-width:190px;",
                        render: function (data, type, row) {
                            return "<a href='@Url.Action("Details")?id=" + data + "'>查看</a>&nbsp;&nbsp;" +
                            "<a href='@Url.Action("Edit")?id=" + data + "'>修改</a>&nbsp;&nbsp;" +
                            "<a href='javascript:void(0)' data-href='@Url.Action("Delete")?id=" + data + "' onclick='Resources.Delete(this);'>删除</a>&nbsp;&nbsp;" +
                            "<a href='javascript:void(0);' data-href='@Url.Action("EditPermission")?id=" + data + "' onclick='Resources.EditPermission(this)'>赋权</a>";
                        }
                    }
                ]
            });
        },
        EditPermission: function (e) {
            $.mainTool.ajaxModal($(e).attr("data-href"), "编辑权限", function (result) {
                Resources.SubmitEditPermission();
            });
        },
        Delete: function (e) {
            $.mainTool.confirm("是否删除该资源？", function (result) {
                $.ajax({
                    type: 'post',
                    url: $(e).attr("data-href"),
                    dataType: "json",
                    data: {
                        '__RequestVerificationToken': $("input[name='__RequestVerificationToken']").val()
                    },
                    success: function (data) {
                        Resources.Search();
                    },
                    error: function (e) {
                        alert(e.responseText);
                    }
                });
            });
        }
    };
</script>
